package org.example.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@WebServlet("/request/json")
public class RequestJSON extends HttpServlet {

    private static final ObjectMapper M = new ObjectMapper();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收请求body为json格式的数据，需要使用inputStream
        InputStream is = req.getInputStream();

//        //接收到的是字节流，需要使用字节字符转换流来转换
//        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
//        //字符流（字节字符转换流（字节流））
//        BufferedReader br = new BufferedReader(isr);
//        String str;
//        //读取一行到字符串，如果不为空，表示没有结束
//        while ((str=br.readLine()) != null){
//            System.out.println(str);
//        }
        User user = M.readValue(is, User.class);
        System.out.println(user);
    }
}
